package ClipboardSubstituter;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JSplitPane;
import java.awt.Dimension;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


public class ApplicationWindow extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	MyClipboardInterface myClipboard=new MyClipboardInterface();
	private JTextArea textAreaLeft;
	private JTextArea textAreaRight;
	private JTextArea textAreaRegex;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ApplicationWindow frame = new ApplicationWindow();
					frame.setVisible(true);
					frame.setAlwaysOnTop(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ApplicationWindow() {
		setTitle("Clipboard replacer v1.0");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 550);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnNewMenu = new JMenu("File");
		menuBar.add(mnNewMenu);
		
		JMenuItem mntmNewMenuItem_1 = new JMenuItem("Save");
		
		mntmNewMenuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				JFileChooser OpenDialog=new JFileChooser();
				if(OpenDialog.showDialog(ApplicationWindow.this, "Save")==JFileChooser.APPROVE_OPTION)
				{
					System.out.println(OpenDialog.getSelectedFile());
					File selectedFile=OpenDialog.getSelectedFile();
					try {
						
						if(selectedFile.exists())
						{
							
							if(JOptionPane.showConfirmDialog(ApplicationWindow.this, "Overwrite?")==JOptionPane.YES_OPTION)
							{
								System.out.println("Yes overwrite");
								selectedFile.delete();
								selectedFile.createNewFile();
								FileWriter saveFile=new FileWriter(selectedFile);
								saveFile.write(textAreaRegex.getText());
								saveFile.close();
							}
							
						}
						else
						{
							System.out.println("File does not exist");
							selectedFile.createNewFile();
							FileWriter saveFile=new FileWriter(selectedFile);
							saveFile.write(textAreaRegex.getText());
							saveFile.close();
							
						}
						
						
					}
				
				catch (IOException e) {
						System.out.println("Something went bananas with file writing");
						e.printStackTrace();
					}
					
			}
				
			}
		});
		mnNewMenu.add(mntmNewMenuItem_1);
		
		JMenuItem mntmNewMenuItem = new JMenuItem("Load");
		
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser OpenDialog=new JFileChooser();
				if(OpenDialog.showOpenDialog(ApplicationWindow.this)==JFileChooser.APPROVE_OPTION)
				{
					System.out.println("Load file window");
					File selectedFile=OpenDialog.getSelectedFile();
					if(selectedFile.exists())
					{
						System.out.println("File exists, attempt of loading contnet will be made");
						try {
							FileReader loadFile=new FileReader(selectedFile);
							char[] chars=new char[(int) selectedFile.length()];
							try {
								loadFile.read(chars);
								textAreaRegex.setText(new String(chars));
								loadFile.close();
								
							} catch (IOException e) {
								System.out.println("Reader cannot read!");
								e.printStackTrace();
							}
							
							
							
						} catch (FileNotFoundException e) {
							System.out.println("File cannot be loaded");
							e.printStackTrace();
						}
						
					}
					else
					{
						JOptionPane.showMessageDialog(ApplicationWindow.this, "File not found !", "Error", JOptionPane.ERROR_MESSAGE); 
					}
					
				}
				
				
			}
		});
		
		mnNewMenu.add(mntmNewMenuItem);
		
		JMenuItem mntmExit = new JMenuItem("Exit");
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		mnNewMenu.add(mntmExit);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		JButton btnNewButton = new JButton("Replace");
		
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
			String regexQueries=textAreaRegex.getText();
			String regexSplited[]=regexQueries.split("\n");
			String clipboardContent=myClipboard.getMyClipboard();
			textAreaLeft.setText(clipboardContent);
			
			for(String currentRegex:regexSplited)
			{
				System.out.println(currentRegex);
				System.out.println(currentRegex.split("=")[0]);
				System.out.println(currentRegex.split("=")[1]);
				clipboardContent=clipboardContent.replaceAll(currentRegex.split("=")[0], currentRegex.split("=")[1]);
					
			}
			textAreaRight.setText(clipboardContent);
			myClipboard.setMyClipboard(clipboardContent);
			}
		});
		
		contentPane.add(btnNewButton, BorderLayout.SOUTH);
		JSplitPane splitPane = new JSplitPane();
		splitPane.setResizeWeight(0.5);
		contentPane.add(splitPane, BorderLayout.CENTER);
		
		textAreaRight = new JTextArea();
		textAreaRight.setLineWrap(true);
		splitPane.setRightComponent(textAreaRight);
		
		textAreaLeft = new JTextArea();
		textAreaLeft.setLineWrap(true);
		splitPane.setLeftComponent(textAreaLeft);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.NORTH);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setPreferredSize(new Dimension(300, 125));
		System.out.println(panel.getSize());
		panel.setLayout(new BorderLayout(0, 0));
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		panel.add(scrollPane);
		
		textAreaRegex = new JTextArea();
		scrollPane.setViewportView(textAreaRegex);
	
	}

	public JTextArea getTextAreaLeft() {
		return textAreaLeft;
	}
	public JTextArea getTextAreaRight() {
		return textAreaRight;
	}
	public JTextArea getTextAreaRegex() {
		return textAreaRegex;
	}
}
